package com.molichuxing.services.util;

import com.molichuxing.framework.utils.EncryptUtil;
import com.molichuxing.services.infrastructure.dto.response.IovResponseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 年检请求工具类
 *
 * @author liweisong
 * @date 2020年5月12日
 *
 */
@Component
public class MotUtils {

    private static Logger logger = LoggerFactory.getLogger(MotUtils.class);

    public static Map<String, String> postHeaderMap = new HashMap<>(); //post请求，请求头参数

    @Value("${mot.secret.key}")
    private String secretKey;

    private final static String PARAM_KEY = "strKey";

    static {
        postHeaderMap.put("Content-Type", "application/json; charset=UTF-8");
    }

    public String doPost(String url,Map<String, Object> paramMap) throws Exception {
        logger.info("op调用第三方接口，接口地址 :: {}", url);
        Map<String, Object> paramMapEn = new HashMap<>();
        String param = paramMapEn(paramMap);
        paramMapEn.put(PARAM_KEY,param);
        logger.info("第三方加密后参数：{}",paramMapEn(paramMap));
        String iovResponseStr = HttpUtil.doPost(url, postHeaderMap, paramMapEn);
        String result = AESCodeUtils.Decrypt(iovResponseStr, secretKey);
        return result;
    }

    //将对象参数转成字符串 - 加密
    public String paramMapEn(Map<String, Object> paramMap) throws Exception {
        String context = JsonUtils.toJson(paramMap);
        return AESCodeUtils.Encrypt(context,secretKey);
    }

    public static void main(String[] args) throws Exception {
        String url = "http://122.114.63.223:8011/ProductRESTService.svc/SetReCheckOrderInfo_pzbl";
//        String iovResponseStr = HttpUtil.doPost(url, postHeaderMap, paramMap);
        String res = AESCodeUtils.Decrypt("WVjb5XKrfCuTK1zju02Ef4Ml3X23MCrc5ziANUOPPbLO4b7pEx8eMLCA9DTic1G8rn3tJppi9peKwWBVXTSjnLo+SIwG03gD6/tyaBfpfM0=", "874591ypznjd@*&&");
        System.out.println(res);
    }
}
